home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / TreeSelectionEvent.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  166 lines

  1. /*
  2.  * @(#)TreeSelectionEvent.java    1.11 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.event;
  22.  
  23. import java.util.EventObject;
  24. import com.sun.java.swing.tree.TreePath;
  25.  
  26. /**
  27.  * An event that characterizes a change in the current
  28.  * selection.  The change is based on any number of paths.
  29.  * TreeSelectionListeners will generally query the source of
  30.  * the event for the new selected status of each potentially
  31.  * changed row.
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.11 04/09/98
  41.  * @author Scott Violet
  42.  * @see TreeSelectionModel
  43.  */
  44. public class TreeSelectionEvent extends EventObject 
  45. {
  46.     /** Paths this event represents. */
  47.     protected TreePath[]     paths;
  48.     /** For each path identifies if that is path is in fact new. */
  49.     protected boolean[]       areNew;
  50.     /** leadSelectionPath before the paths changed, may be null. */
  51.     protected TreePath        oldLeadSelectionPath;
  52.     /** leadSelectionPath after the paths changed, may be null. */
  53.     protected TreePath        newLeadSelectionPath;
  54.  
  55.     /**
  56.       * Represents a change in the selection of a TreeSelectionModel.
  57.       * paths identifies the paths that have been either added or
  58.       * removed from the selection.
  59.       *
  60.       * @param source source of event
  61.       * @param paths the paths that have changed in the selection
  62.       */
  63.     public TreeSelectionEvent(Object source, TreePath[] paths,
  64.                   boolean[] areNew, TreePath oldLeadSelectionPath,
  65.                   TreePath newLeadSelectionPath)
  66.     {
  67.     super(source);
  68.     this.paths = paths;
  69.     this.areNew = areNew;
  70.     this.oldLeadSelectionPath = oldLeadSelectionPath;
  71.     this.newLeadSelectionPath = newLeadSelectionPath;
  72.     }
  73.  
  74.     /**
  75.       * Represents a change in the selection of a TreeSelectionModel.
  76.       * path identifies the path that have been either added or
  77.       * removed from the selection.
  78.       *
  79.       * @param source source of event
  80.       * @param path the path that has changed in the selection
  81.       * @param isNew whether or not the path is new to the selection, false
  82.       * means path was removed from the selection.
  83.       */
  84.     public TreeSelectionEvent(Object source, TreePath path, boolean isNew,
  85.                   TreePath oldLeadSelectionPath,
  86.                   TreePath newLeadSelectionPath)
  87.     {
  88.     super(source);
  89.     paths = new TreePath[1];
  90.     paths[0] = path;
  91.     areNew = new boolean[1];
  92.     areNew[0] = isNew;
  93.     this.oldLeadSelectionPath = oldLeadSelectionPath;
  94.     this.newLeadSelectionPath = newLeadSelectionPath;
  95.     }
  96.  
  97.     /**
  98.       * Returns the paths that have been added or removed from the
  99.       * selection.
  100.       */
  101.     public TreePath[] getPaths()
  102.     {
  103.     int                  numPaths;
  104.     TreePath[]          retPaths;
  105.  
  106.     numPaths = paths.length;
  107.     retPaths = new TreePath[numPaths];
  108.     System.arraycopy(paths, 0, retPaths, 0, numPaths);
  109.     return retPaths;
  110.     }
  111.  
  112.     /**
  113.       * Returns the first path element.
  114.       */
  115.     public TreePath getPath()
  116.     {
  117.     return paths[0];
  118.     }
  119.  
  120.     /**
  121.      * Returns true if the first path element has been added to the
  122.      * selection, a return value of false means the first path has been
  123.      * removed from the selection.
  124.      */
  125.     public boolean isAddedPath() {
  126.     return areNew[0];
  127.     }
  128.  
  129.     /**
  130.      * Returns true if the path identified by path was added to the
  131.      * selection. A return value of false means the path was in the
  132.      * selection but is no longer in the selection. This will raise if
  133.      * path is not one of the paths identified by this event.
  134.      */
  135.     public boolean isAddedPath(TreePath path) {
  136.     for(int counter = paths.length - 1; counter >= 0; counter--)
  137.         if(paths[counter].equals(path))
  138.         return areNew[counter];
  139.     throw new IllegalArgumentException("path is not a path identified by the TreeSelectionEvent");
  140.     }
  141.  
  142.     /**
  143.      * Returns the path that was previously the lead path.
  144.      */
  145.     public TreePath getOldLeadSelectionPath() {
  146.     return oldLeadSelectionPath;
  147.     }
  148.  
  149.     /**
  150.      * Returns the current lead path.
  151.      */
  152.     public TreePath getNewLeadSelectionPath() {
  153.     return newLeadSelectionPath;
  154.     }
  155.  
  156.     /**
  157.      * Returns a copy of the receiver, but with the source being newSource.
  158.      */
  159.     public Object cloneWithSource(Object newSource) {
  160.       // Fix for IE bug - crashing
  161.       return new TreeSelectionEvent(newSource, paths,areNew,
  162.                     oldLeadSelectionPath,
  163.                     newLeadSelectionPath);
  164.     }
  165. }
  166.